package models

import (
	"errors"

	"coinsend/btc"
	"coinsend/eth"
	"coinsend/ltc"
	"coinsend/usdt"
	"coinsend/utils"
	"strings"

	"github.com/astaxie/beego/orm"
)

//获取待签名串
func GetActiveUnsigne(Symbol, From, To string, Amount float64) (string, error) {

	//判断币种属性
	Type, err1 := ORMGetCoinType(Symbol)

	//检查地址合法性
	var Symbol0 = Symbol
	if Type == 10 {
		Symbol0 = "ETH"
	}
	if !utils.CheckAddress(Symbol0, To) {
		return "", errors.New("获取失败," + Symbol + "目的地址不合法")
	}

	//获取矿工费
	Contract, Fee, err := ORMGetCoinFeeContract(Symbol)
	if err != nil {
		return "", err
	}
	if err1 != nil {
		return "", errors.New("获取失败," + Symbol + "币种信息不存在" + err1.Error())
	}

	if Type == 10 {

		Nonce, err11 := eth.GetETHNonce(From)
		if err11 != nil {
			return "", errors.New("获取发币地址Nonce出错" + err11.Error())
		}

		if Symbol == "ETH" {

			unsigne, err := eth.GetEthUnSigneString(uint64(Nonce), From, To, Amount, Fee)
			if err != nil {
				return "", errors.New("生成待签名串失败,原因" + err.Error())
			}
			return unsigne, nil

		} else {
			//代币

			unsigne, err := eth.GetEthTokenUnSigneString(uint64(Nonce), From, To, Contract, Symbol, Amount, Fee, 60000.0)
			if err != nil {
				return "", errors.New("生成待签名串失败,原因" + err.Error())
			}
			return unsigne, nil
		}
	} else if Symbol == "BTC" {
		unsigne, err11 := btc.PayTo(From, To, Amount, Fee)
		if err11 != nil {
			return "", err11
		}
		return unsigne, nil
	} else if Symbol == "LTC" {
		unsigne, err11 := ltc.PayTo(From, To, Amount, Fee)
		if err11 != nil {
			return "", err11
		}
		return unsigne, nil
	} else if Symbol == "USDT" {
		unsigne, err11 := usdt.GetUsdtUnsign(From, To, Amount, Fee)
		if err11 != nil {
			return "", err11
		}
		return unsigne, nil
	} else {
		return "", errors.New("币种暂未支持")
	}

}

//广播事务
func SendActiveSigned(To, Symbol, Signed string) (string, error) {
	//判断币种属性
	Type, err1 := ORMGetCoinType(Symbol)
	if err1 != nil {
		return "", errors.New("广播失败," + Symbol + "币种信息不存在" + err1.Error())
	}

	if Type == 10 {
		if strings.Contains(strings.ToLower(Signed), strings.ToLower(To[2:])) == false {
			return "", errors.New("广播失败,目的地址不匹配")
		}

		hash, err := eth.SendEthTransaction(Signed)
		if err != nil {
			return "", errors.New("广播失败," + err.Error())
		} else {
			return hash, nil
		}
	} else if Symbol == "BTC" || Symbol == "USDT" {

		ok, err11 := btc.CheckBTC(Signed, To)
		if err11 != nil {
			return "", errors.New("广播失败,检查目的地址出错" + err11.Error())
		} else if !ok {

			return "", errors.New("广播失败,目的地址与订单地址不匹配")
		}
		hash, err11 := btc.Broadcast(Signed)
		if err11 != nil {
			return "", err11
		}
		return hash, nil
	} else if Symbol == "LTC" {

		hash, err11 := ltc.Broadcast(Signed)
		if err11 != nil {
			return "", err11
		}
		return hash, nil
	} else {
		return "", errors.New("广播失败," + Symbol + "暂未支持")
	}

}

type AddressList struct {
	Address string
	Symbol  string
	Id      int
}

//获取币种列表
func ORMActiveListAddress(uid int) ([]AddressList, error) {
	var alladdress []Address
	var addressamount []AddressList

	o := orm.NewOrm()
	o.QueryTable("Address").Filter("Uid", uid).All(&alladdress)
	for _, v := range alladdress {
		var addressamount0 AddressList

		addressamount0.Address = v.Address
		addressamount0.Id = v.Id

		if v.Symbol == "ETH" {
			coin, _ := ORMListCoinByType(10)
			for _, val := range coin {
				addressamount0.Symbol = val.Symbol
				addressamount = append(addressamount, addressamount0)
			}
		} else if v.Symbol == "BTC" {
			addressamount0.Symbol = v.Symbol
			addressamount = append(addressamount, addressamount0)
		} else {
			addressamount0.Symbol = v.Symbol
			addressamount = append(addressamount, addressamount0)

		}

	}

	return addressamount, nil

}
